package com.woniuxy.main.utils;

import java.math.BigDecimal;

/**
 * BigDecimal自定义 操作枚举类
 *
 * @author Su
 */
public class OperationBigDecimal {

    /**
     * BigDecimal加减乘除运算
     *
     * @param numOne        [可为String Integer Long Double BigDecimal]
     * @param numTwo        [可为String Integer Long Double BigDecimal]
     * @param operationEnum 运算方式
     * @param scale         希望保留的小数
     * @param roundingMode  BigDecimal.ROUND_DOWN  向零方向舍入 详见BigDecimal字段
     *                      BigDecimal.ROUND_UP    向远离0的方向舍入
     * @return
     * @throws Exception 运算异常
     */
    public static BigDecimal operationASMD(Object numOne, Object numTwo,
                                           OperationEnum operationEnum,
                                           int scale,
                                           int roundingMode) throws Exception {
        BigDecimal num1 = new BigDecimal(String.valueOf(numOne)).setScale(scale, roundingMode);
        BigDecimal num2 = new BigDecimal(String.valueOf(numTwo)).setScale(scale, roundingMode);
        switch (operationEnum) {
            case ADD:
                return num1.add(num2).setScale(scale, roundingMode);
            case SUBTRACT:
                return num1.subtract(num2).setScale(scale, roundingMode);
            case MULTIPLY:
                return num1.multiply(num2).setScale(scale, roundingMode);
            case DIVIDE:
                return num1.divide(num2, scale, roundingMode);
            default:
                return null;
        }
    }
}